package database;

// <editor-fold defaultstate="collapsed" desc="HeaderFiles">
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;// </editor-fold>

/**
 * @version
 *
 * @Date Mar 30, 2011
 * @author Rajeeb Saha
 *
 */
public class CustomerDAO {

    // <editor-fold defaultstate="collapsed" desc="Private Variables">
    private Connection conn = null;
    private Statement sqlStatement;
    private static final Logger logger = Logger.getLogger(
            CustomerDAO.class.getName());
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructor">
    /**
     *
     */
    public CustomerDAO() {
        ConnectionDAO connectionDAO = new ConnectionDAO();
        conn = connectionDAO.getConn();
        sqlStatement = connectionDAO.getSqlStatement();
    }

    /**
     *
     * @param conn
     * @param sqlStatement
     */
    public CustomerDAO(Connection conn, Statement sqlStatement) {
        this.conn = conn;
        this.sqlStatement = sqlStatement;
    }// </editor-fold>

    /**
     *
     * @param password
     * @param firstName
     * @param lastName
     * @param email
     * @param phone
     * @return
     */
    protected String registerCustomer(String password, String firstName,
            String lastName, String email, String phone) {
        String csid = "";

        try {
            String sqlText = "select max(c.csid) from digistore.customer c";

            ResultSet results = sqlStatement.executeQuery(sqlText);

            results.next();
            csid = results.getString(1);

            if (csid == null) {
                csid = "1000001";
            } else {
                csid = "" + (Integer.parseInt(csid.trim()) + 1);
            }

            //  System.out.print(csid);

            sqlText =
                    "insert into digistore.customer " +
                    "(ckid, passcode, firstName, lastName, email, phone) values " +
                    "(" + "'" + csid + "'," + "'" + password + "'," + "'" +
                    firstName + "'," + "'" + lastName + "'," + "'" + email +
                    "'," + "'" + phone + "')";

            int value = sqlStatement.executeUpdate(sqlText);
            if (value == 1) {
                conn.commit();
                return csid;
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, "Customer : regesterCustomer" + ex);
            //    throw ex;
        }
        return csid;
    }

    /**
     *
     * @param csid
     * @return
     */
    protected boolean deleteCustomer(int csid) {
        try {
            String sqlText = "DELETE FROM digistore.customer WHERE csid=" + csid;
            int value = sqlStatement.executeUpdate(sqlText);
            if (value == 1) {
                conn.commit();
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, "CustomerDAO : deleteCustomer" + ex);
        }
        return true;
    }

    /**
     *
     * @param csid
     * @return
     */
    protected String getPassword(int csid) {
        String resultString = "";
        try {
            String sqlText =
                    "select c.passcode " + "from digistore.customer c " +
                    "where c.ckid = '" + csid + "'";
            ResultSet results = sqlStatement.executeQuery(sqlText);
            if (!results.next()) { // Check user name
                resultString = "";
            } else {
                resultString = results.getString(1);
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return resultString;
    }

    /**
     *
     * @param csid
     * @param password
     * @param firstName
     * @param lastName
     * @param email
     * @param phone
     * @return
     */
    protected boolean updateCustomer(int csid, String password, String firstName,
            String lastName, String email, String phone) {
        try {
            String sqlText = "UPDATE digistore.customer SET passcode='" +
                    password + "', " + "firstName='" + firstName + "', " +
                    "lastName='" + lastName + "', " + "email='" + email +
                    "', " + "phone='" + phone + "' WHERE csid='" + csid + "'";
            int value = sqlStatement.executeUpdate(sqlText);
            if (value == 1) {
                conn.commit();
                return true;
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return false;
    }
    /**
     * get address list
     */
    /**
     * add address
     */
    /**
     * update address
     */
    /**
     * delete address
     */
    /**
     * view order list
     */
    /**
     * view total store card discount
     */
}
